import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

/// @date 2020/08/31  11:25
/// @author 程志强
/// @des 国际化配置

// locale资源类
class AppLocalizations {
  // 是否为中文
  bool isZh = false;

  AppLocalizations(this.isZh);

  // 静态方法获取DemoLocalizations
  static AppLocalizations of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations);
  }

  // title标题设置
  String get title {
    return isZh ? "实现Localizations" : "implement Localizations";
  }
}

//Locale代理类
class AppLocalizationsDelegate
    extends LocalizationsDelegate<AppLocalizations> {
  // 是否支持某个Local
  @override
  bool isSupported(Locale locale) => ['en', 'zh'].contains(locale.languageCode);

  // Flutter 会调用此类加载相应的Locale资源类
  @override
  Future<AppLocalizations> load(Locale locale) {
    return SynchronousFuture<AppLocalizations>(
        AppLocalizations(locale.languageCode == 'zh'));
  }

  @override
  bool shouldReload(LocalizationsDelegate<AppLocalizations> old) => false;
}